package entities;





import java.awt.Graphics;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Composite implements Drawable{
    private ArrayList<Drawable> componentes;
    private Semaphore semaphore;
    
    public Composite(){
        componentes = new ArrayList<>();
        semaphore = new Semaphore(1);
    }

    @Override
    public void render(Graphics g) {
        try {
            semaphore.acquire();
            for (Drawable d : componentes) d.render(g);
            semaphore.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(Composite.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    @Override
    public void add(Drawable d) {
        try {
            semaphore.acquire();
            componentes.add(d);
            semaphore.release();
        } catch (InterruptedException ex) {
            Logger.getLogger(Composite.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void remove(Drawable d) {
        componentes.remove(d);
    }

    @Override
    public Drawable getChild(int i) {
        return componentes.get(i);
    }

    @Override
    public Drawable colision(Drawable d) {
        for (Drawable drawable : componentes) drawable.colision(d);
        return null;
    }
    
    public int getSize(){
        return componentes.size();
    }
}
